﻿using System;
using System.Data.Common;

namespace Centido.Management.ConsoleManager
{
    /// <summary>
    ///   A console command that deletes the specified security role from the current domain
    /// or removes the role from the specified parent security role.
    /// </summary>
    internal class DeleteRoleCommand : Command
    {
        /// <summary>
        ///  The role name parameter.
        /// </summary>
        public const string RoleNameParameter = "role";

        /// <summary>
        ///  The role name parameter (short name).
        /// </summary>
        public const string RoleNameShortParameter = "r";

        /// <summary>
        ///   The parent role name parameter.
        /// </summary>
        public const string ParentRoleNameParameter = "parentrole";

        /// <summary>
        ///   The parent role name parameter (short name).
        /// </summary>
        public const string ParentRoleNameShortParameter = "pr";


        /// <summary>
        ///   Gets the console command name.
        /// </summary>
        public override string Name
        {
            get { return "deleterole"; }
        }


        /// <summary>
        ///   Gets the console command description.
        /// </summary>
        public override string Description
        {
            get { return Messages.DeleteRoleCommandDescription; }
        }


        /// <summary>
        ///   Gets the console command full description.
        /// </summary>
        public override string FullDescription
        {
            get { return Messages.DeleteRoleCommandFullDescription; }
        }


        /// <summary>
        ///   Executes the console command.
        /// </summary>
        /// <param name="context">The console command execution context.</param>
        /// <exception cref="ArgumentNullException">Thrown when the value of <paramref name="context"/> is null.</exception>
        public override void Execute(CommandExecutionContext context)
        {
            base.Execute(context);

            // If the role name was not specified
            if (!context.ParameterValueIsNotEmpty(RoleNameParameter, RoleNameShortParameter))
            {
                Console.WriteLine(Messages.DeleteRoleCommandNotAllParametersSpecifiedError);
                return;
            }

            var domain = context.CurrentDomain;

            if (domain == null) return;

            // If the current user has no permission to perform this action
            if (!domain.CurrentUser.CanChangeSecurity)
            {
                Console.WriteLine(Messages.NeedCanChangeSecurityPermissionMessage);
                return;
            }

            // The role name
            var roleName = context.GetParameterValue(RoleNameParameter, RoleNameShortParameter);

            try
            {
                // Trying to find the role
                var role = domain.GetRole(roleName);

                // If the role was not found
                if (role == null)
                {
                    Console.WriteLine(Messages.CannotFindRoleError, roleName);
                    return;
                }

                // If the parent role was specified
                if (context.ParameterValueIsNotEmpty(ParentRoleNameParameter, ParentRoleNameShortParameter))
                {
                    // ... then deleting the role from the parent role
                    var parentRoleName = context.GetParameterValue(ParentRoleNameParameter, ParentRoleNameShortParameter);

                    // Trying to find the parent role
                    var parentRole = domain.GetRole(parentRoleName);

                    // If the parent role was not found
                    if (parentRole == null)
                    {
                        Console.WriteLine(Messages.CannotFindRoleError, parentRoleName);
                        return;
                    }

                    if (parentRole.ContainsRole(role))
                        parentRole.RemoveRole(role);
                }
                // If the parent role was not specified
                else
                {
                    // ... then deleting the role
                    role.Delete();
                }
            }
            catch (DbException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine();
                return;
            }

            // Displaying the success message
            Console.WriteLine(Messages.CommandCompletedMessage);
        }
    }
}
